#include "myDialog.h"
#include <QFileInfo>

myDialog::myDialog()
{
	//控件初始化
	fileNameEdit = new QLineEdit();
	openBtn = new QPushButton(tr("打开"));
	infoText = new QTextEdit();
	getBtn = new QPushButton(tr("获取文件信息"));
	dir = new QCheckBox(tr("目录"));
	readable = new QCheckBox(tr("可读"));
	writeable = new QCheckBox(tr("可 写"));
	executeable = new QCheckBox(tr("可执行"));

	//布局
	QHBoxLayout *hLay0 = new QHBoxLayout();
	hLay0->addWidget(new QLabel(tr("文件名")));
	hLay0->addWidget(fileNameEdit);
	hLay0->addWidget(openBtn);
	
	QHBoxLayout *hLay1 = new QHBoxLayout();
	hLay1->addWidget(dir);
	hLay1->addWidget(readable);
	hLay1->addWidget(writeable);
	hLay1->addWidget(executeable);

	QVBoxLayout *vLay = new QVBoxLayout();
	vLay->addLayout(hLay0);
	vLay->addWidget(infoText);
	vLay->addLayout(hLay1);
	vLay->addWidget(getBtn);

	//绑定信号和槽函数
	connect(openBtn, SIGNAL(clicked()), this, SLOT(openFile()));
	connect(getBtn, SIGNAL(clicked()), this, SLOT(getInfo()));
	setLayout(vLay);
}

void myDialog::openFile()
{
	QString path = QFileDialog::getOpenFileName(this, tr("选择文件"), "/", "file(*)");
	fileNameEdit->setText(path);
}	
void myDialog::getInfo()
{
	//fileNameEdit  infoText   dir   readable writeable executeable
	//获取QFileInfo对象
	QFileInfo fileInfo(fileNameEdit->text());
	//清空所有的信息
	infoText->clear();
	//从QFileinfo对象中获取信息
	qint64 size = fileInfo.size();
	QDateTime created = fileInfo.created();
	QDateTime lastModified = fileInfo.lastModified();
	QDateTime lastRead = fileInfo.lastRead();
	bool isDir = fileInfo.isDir();
	bool isReadable = fileInfo.isReadable();
	bool isWriteable = fileInfo.isWritable();
	bool isExecutable = fileInfo.isExecutable();

	//加载所有信息
	infoText->append(QString(tr("file size: %1\n")).arg(size));
	infoText->append(QString(tr("file create time: %1\n")).arg(created.toString()));
	infoText->append(QString(tr("file lastModified time: %1\n")).arg(lastModified.toString()));
	infoText->append(QString(tr("file lastRead time: %1\n")).arg(lastRead.toString()));
	//set checkbox
	dir->setCheckState(isDir?Qt::Checked :Qt::Unchecked);
	readable->setCheckState(isReadable?Qt::Checked :Qt::Unchecked);
	writeable->setCheckState(isWriteable?Qt::Checked :Qt::Unchecked);
	executeable->setCheckState(isExecutable?Qt::Checked :Qt::Unchecked);
}


